package csxXiti.xiti4;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author WuYongheng
 * @date 2021/11/21
 * @description 给定一个 list 集合:{"张柏","刘华","张案","张颖","杨丽","黄晓"}
 * （1）编写一个方法:将 list 集合中所有姓张的人员写入到 E:\\user.txt 中
 * （2）编写一个方法:将 E:\\user.txt 中所有姓张的人员信息读取出来并打印到控制台上。
 */
public class xiti4_4 {
    public static void main(String[] args) throws IOException {
        List<String> list = new ArrayList<>();
        Collections.addAll(list, "张柏", "刘华", "张案", "张颖", "杨丽", "黄晓");
        writer(list);
        read(list);
    }

    static void writer(List<String> list) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\user.txt"));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).substring(0, 1).equals("张")) {
                bw.write(list.get(i) + "");
            }
        }
        bw.close();
    }

    static void read(List<String> list) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\user.txt"));
        String line = br.readLine();
        System.out.println(line);
        br.close();
    }
}


